package com.hnevc.foodserver.service;

import com.hnevc.foodserver.bean.Comments;
import com.hnevc.foodserver.bean.CommentsPageVo;
import com.hnevc.foodserver.bean.Foods;
import com.hnevc.foodserver.bean.Users;
import com.hnevc.foodserver.dao.CommentsDao;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsService {
    @Autowired
    CommentsDao commentsDao;

    public Comments save(Comments comments) {
        Comments save = commentsDao.save(comments);
        return save;
    }

    public List<Comments> list() {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        List<Comments> all = commentsDao.findAll(sort);
        return all;
    }


    public List<Comments> getCommentsByFoodId(Comments comments) {
        Example example = Example.of(comments);
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        List<Comments> all = commentsDao.findAll(example,sort);
        return all;
    }

    public Comments findone(Comments comments) {

        Comments com= commentsDao.findOne(Example.of(comments)).orElse(null);
        return com;
    }


    public Page<Comments> getCommentsByFoodIdByPage(PageRequest page) {

        Page<Comments> all = commentsDao.findAll(page);
        return all;
    }

    public void deleteById(Integer id) {
        commentsDao.deleteById(id);
    }

    public Comments updateComment(Comments comments) {
        Comments save = commentsDao.save(comments);
        return save;
    }


}
